package cn.yougou.controller;

import cn.yougou.common.DtoUtil;
import cn.yougou.dto.Dto;
import cn.yougou.dto.CommodityAllinone;
import cn.yougou.dto.Query;
import cn.yougou.pojo.YgCommodity;
import cn.yougou.service.admin.YgAdminCommodityService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * (YgAdminCommodity)表控制层
 *
 * @author 白杰崇
 * @since 2020-12-03 01:32:19
 */
@RestController
@RequestMapping("ygAdminCommodity")
public class YgAdminCommodityController {

    @Reference
    private YgAdminCommodityService ygAdminCommodityService;

    /**
     * 查询全部商品(分页)
     * @return
     */
    @PostMapping("/findAll")
    public Dto findAll(@RequestBody Query query){
        //计算当前从多少条展示
        query.setPage((query.getPage()-1)*query.getLimit());
        //逻辑删除
        query.setDeleted(1);
        //根据条件查询总条数赋值给query
        query.setInfoCount(ygAdminCommodityService.selCount(query));
        return DtoUtil.returnDataListSuccess(ygAdminCommodityService.findAllByQuery(query),query);
    }

    /**
     * 添加商品
     * @param commodityAllinone
     * @return
     */
    @PostMapping("/create")
    public Object create(@RequestBody CommodityAllinone commodityAllinone){
        Dto dto = null;
        YgCommodity ygCommodity = commodityAllinone.getCommodity();
        if(ygCommodity.getCommodityid() == null || ygCommodity.getCommoditymoney() == null
           || ygCommodity.getCategoryid() == null || ygCommodity.getPicurl() == null){
            dto = DtoUtil.returnFail("缺少商品信息","50001");
        }else{
            //添加
            dto = ygAdminCommodityService.create(commodityAllinone);
        }
        return dto;
    }

    /**
     * 删除商品
     * @param ygCommodity
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody YgCommodity ygCommodity){
        Dto dto = null;
        if(ygCommodity.getId() == null){
            dto = DtoUtil.returnFail("删除失败","50001");
        }else{
            //删除逻辑
            dto = ygAdminCommodityService.delete(ygCommodity);
        }
        return dto;
    }


    /**
     * 更新商品
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody CommodityAllinone commodityAllinone){
        Dto dto = null;
        YgCommodity ygCommodity = commodityAllinone.getCommodity();
        if(ygCommodity.getCommodityid() == null || ygCommodity.getCommoditymoney() == null
            || ygCommodity.getCategoryid() == null || ygCommodity.getPicurl() == null){
            dto = DtoUtil.returnFail("缺少商品信息","50001");
        }else{
            dto = ygAdminCommodityService.update(commodityAllinone);
        }
        return dto;
    }

    /**
     * 根据id获取商品信息，规格，规格组合
     * @return
     */
    @GetMapping("/detail")
    public Dto detail(Integer id){
        if(null == id){
            return DtoUtil.returnFail("缺少必要参数","50001");
        }
        Dto dto = ygAdminCommodityService.detail(id);
        return dto;
    }











}
